﻿using MailDAL;
using MailTool.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MailTool.Pages
{
    public partial class TemplateEdit : System.Web.UI.Page
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
                _FileBrowser.BasePath = "/ckfinder/";
                _FileBrowser.SetupCKEditor(this.templateContentCKeditor);
                InitPageData();
            }
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void InitPageData()
        {
            if (Action == "add")
            {
                lblCreateDate.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
                lblUserName.Text = CurrentUser.UserName;
            }
            else
            {
                MailModel.Template template = unitOfWork.TemplateRepository.Get(p => p.Id == TemplateID, null, 1, 0, x => x.User).FirstOrDefault();
                txtTemplateName.Text = template.TemplateName;
                txtTemplateTitle.Text = template.TemplateTitle;
                templateContentCKeditor.Text = Server.HtmlDecode(template.TemplateContent);
                lblUserName.Text = template.User.UserName;
                lblCreateDate.Text = template.CreateDate.ToString("yyyy-MM-dd hh:mm");
            }
        }

        protected void BtnEditArticle_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTemplateName.Text))
            {
                this.LblError.Text = "<font color='red'>模板名称不能为空！</font>";
                return;
            }
            else if (string.IsNullOrEmpty(txtTemplateTitle.Text))
            {
                this.LblError.Text = "<font color='red'>标题模板不能为空！</font>";
                return;
            }
            else if (string.IsNullOrEmpty(templateContentCKeditor.Text))
            {
                this.LblError.Text = "<font color='red'>模板内容不能为空！</font>";
                return;
            }
            try
            {
                if (Action == "add")
                {
                    MailModel.Template template = new MailModel.Template();
                    template.TemplateName = txtTemplateName.Text.Trim();
                    template.TemplateTitle = txtTemplateTitle.Text.Trim();
                    template.TemplateContent = Server.HtmlEncode(templateContentCKeditor.Text);
                    template.CreateDate = DateTime.Now;
                    template.UpdateDate = DateTime.Now;
                    template.UserId = SessionState.CurrentUser.Id;
                    unitOfWork.TemplateRepository.Insert(template);
                    unitOfWork.Save();
                }
                else
                {
                    MailModel.Template template = unitOfWork.TemplateRepository.Get(p => p.Id == TemplateID, null, 1, 0, x => x.User).FirstOrDefault();
                    template.TemplateName = txtTemplateName.Text.Trim();
                    template.TemplateTitle = txtTemplateTitle.Text.Trim();
                    template.TemplateContent = Server.HtmlEncode(templateContentCKeditor.Text);
                    template.CreateDate = DateTime.Now;
                    template.UpdateDate = DateTime.Now;
                    unitOfWork.Save();
                }
                JsUtil.AlertConfirm("活动保存成功！", this.Page);
                JsUtil.RefreshParent("TemplateList.aspx", this.Page);
            }
            catch
            {
                JsUtil.Alert("保存失败！", this.Page);
            }
        }

        protected int TemplateID
        {
            get
            {
                return Convert.ToInt32(base.Request["templateId"]);
            }
        }

        protected string Action
        {
            get
            {
                return base.Request["action"];
            }
        }

        protected MailModel.User CurrentUser
        {
            get
            {
                return SessionState.CurrentUser;
            }
        }
    }
}